package control;

/*
 * Clase encargada de conectar con los servicios y mapear el objeto usuario
 */

import java.util.ArrayList;
import java.util.List;
import java.security.MessageDigest;

import mapping.Sucursal;
import mapping.Usuario;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.example.goatstore.General;

public class ControlSucursal 
{
	private static String SOAP_ACTION;
	private static String METHOD_NAME;
	private static final String NAMESPACE = "http://servicioSucursal/";
	private static final String URL = General.getIP()+":8080/ServiciosTaller/SucursalWebService?wsdl";
	
	public static Sucursal makeSucursal(SoapObject result)
	{
		Sucursal su=null;
		long id = Long.parseLong(result.getProperty("id").toString());
		String direccion = result.getProperty("direccion").toString();
		Float latitud = Float.parseFloat(result.getProperty("latitud").toString());
		Float longitud = Float.parseFloat(result.getProperty("longitud").toString());
		Usuario usuario=ControlUsuario.makeUsuario((SoapObject)result.getProperty("usuario"));   
		su=new Sucursal(id, direccion, latitud, longitud, usuario);	
		return su;
	}
	
	public static void setCall(int metodo)
	{
		switch(metodo) 
		{
		    case 1:  
			    METHOD_NAME="listarSucursal";
			    SOAP_ACTION="http://servicioSucursal/listarSucursal";
		    break;
		    case 2:  
			    METHOD_NAME="crearSucursal";
			    SOAP_ACTION="http://servicioSucursal/crearSucursal";
		    break;
		    case 3:  
				  METHOD_NAME="obtenerSucursalId";
				  SOAP_ACTION="http://servicioSucursal/obtenerSucursalId";
			break;
		    case 4:  
				  METHOD_NAME="eliminarSucursal";
				  SOAP_ACTION="http://servicioSucursal/eliminarSucursal";
			break;
		    case 5:  
				  METHOD_NAME="actualizarSucursal";
				  SOAP_ACTION="http://servicioSucursal/actualizarSucursal";
			break;
		}
	}
	
	public static List<Sucursal> listarSucursal(long idempresa)
	{
        setCall(1);
        List<Sucursal> su;
		try 
		{
	       //Creaccion de la Solicitud
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			request.addProperty("idempresa",idempresa);
			//Creaccion del Envelope
			SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			sobre.dotNet = false;
			sobre.setOutputSoapObject(request);
			//Creaccion del transporte
			HttpTransportSE transporte = new HttpTransportSE(URL);
			//Llamada
			transporte.call(SOAP_ACTION, sobre);
			//Resultado
			SoapObject resultsRequestSOAP = (SoapObject)sobre.bodyIn;
			su=new ArrayList<Sucursal>();
			Sucursal s;
			for(int i=0;i<resultsRequestSOAP.getPropertyCount();i++){
				SoapObject object=(SoapObject) resultsRequestSOAP.getProperty(i);
				s=makeSucursal(object);
				su.add(s);
			}	
        }catch (Exception e) 
        {
        	su=null;
        }
		return su;
	}
	
	public static boolean crearSucursal(String direccion, float latitud, float longitud, long idempresa)
	{
        setCall(2);
        boolean res;
		try 
		{
			//Creaccion de la Solicitud
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			request.addProperty("direccion",direccion);
			request.addProperty("latitud",latitud);
			request.addProperty("longitud",longitud);
			request.addProperty("id",idempresa);
			//Creaccion del Envelope
			SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			sobre.dotNet = false;
			sobre.setOutputSoapObject(request);
			//Creaccion del transporte
			HttpTransportSE transporte = new HttpTransportSE(URL);
			//Llamada
			transporte.call(SOAP_ACTION, sobre);
			//Resultado
			SoapObject resultsRequestSOAP = (SoapObject)sobre.bodyIn;
			res = Boolean.parseBoolean(resultsRequestSOAP.getProperty(0).toString());
        }catch (Exception e) 
        {
        	res=false;
        }
		return res;
	}
	
	public static Sucursal obtenerSucursal(long id)
	{
        setCall(3);
        Sucursal s;
        try 
        {
 	       //Creaccion de la Solicitud
 			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
 			request.addProperty("id",id);
 			//Creaccion del Envelope
 			SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
 			sobre.dotNet = false;
 			sobre.setOutputSoapObject(request);
 			//Creaccion del transporte
 			HttpTransportSE transporte = new HttpTransportSE(URL);
 			//Llamada
 			transporte.call(SOAP_ACTION, sobre);
 			//Resultado
 			SoapObject resultsRequestSOAP = (SoapObject)sobre.bodyIn;
 			SoapObject object=(SoapObject) resultsRequestSOAP.getProperty(0);
 			s=makeSucursal(object); 
         }catch (Exception e) 
         {
         	s=null;
         }
 		return s;
	}
	
	public static boolean eliminarSucursal(long id)
	{
        setCall(4);
        boolean res;
		try 
		{
	       //Creaccion de la Solicitud
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			request.addProperty("id",id);
			//Creaccion del Envelope
			SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			sobre.dotNet = false;
			sobre.setOutputSoapObject(request);
			//Creaccion del transporte
			HttpTransportSE transporte = new HttpTransportSE(URL);
			//Llamada
			transporte.call(SOAP_ACTION, sobre);
			//Resultado
			SoapObject resultsRequestSOAP = (SoapObject)sobre.bodyIn;
			res = Boolean.parseBoolean(resultsRequestSOAP.getProperty(0).toString());
        }catch (Exception e) 
        {
        	res=false;
        }
		return res;
	}
	
	
	public static boolean actualizarSucursal(long id, String direccion, float latitud, float longitud)
	{
        setCall(5);
        boolean res;
		try 
		{
	       //Creaccion de la Solicitud
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			request.addProperty("id",id);
			request.addProperty("direccion",direccion);
			request.addProperty("latitud",latitud);
			request.addProperty("longitud",longitud);
			//Creaccion del Envelope
			SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			sobre.dotNet = false;
			sobre.setOutputSoapObject(request);
			//Creaccion del transporte
			HttpTransportSE transporte = new HttpTransportSE(URL);
			//Llamada
			transporte.call(SOAP_ACTION, sobre);
			//Resultado
			SoapObject resultsRequestSOAP = (SoapObject)sobre.bodyIn;
			res = Boolean.parseBoolean(resultsRequestSOAP.getProperty(0).toString());
        }catch (Exception e) 
        {
        	res=false;
        }
		return res;
	}
	
}
